Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow injecting command args/options into __invoke() #25

Merged
merged 1 commit into from
Jun 30, 2022

Conversation

kbond
Copy link
Member

@kbond kbond commented Jun 29, 2022

class MyCommand extends \Symfony\Component\Console\Command\Command
{
    use Invokable;

    // $username/$roles are the argument/option defined below
    public function __invoke(IO $io, string $username, array $roles)
    {
        $io->success('created.');

        // even if you don't inject IO, it's available as a method:
        $this->io(); // IO
    }

    public function configure(): void
    {
        $this
            ->addArgument('username', InputArgument::REQUIRED)
            ->addOption('roles', mode: InputOption::VALUE_IS_ARRAY)
        ;
    }
}

@kbond kbond added the enhancement New feature or request label Jun 29, 2022
src/Invokable.php Outdated Show resolved Hide resolved
@kbond kbond marked this pull request as ready for review June 30, 2022 13:21
@kbond kbond merged commit a1e132f into zenstruck:1.x Jun 30, 2022
@kbond kbond deleted the inject-arg-option branch June 30, 2022 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

1 participant